package com.tongan.generate.tinyextend;

import com.thoughtworks.xstream.XStream;
import com.tongan.generate.CgformCodeFactory;
import com.tongan.util.CodeResourceUtil;
import org.tinygroup.xstream.XStreamFactory;

import java.io.File;
import java.nio.file.Paths;

/**
 * Created by kenti on 2017/6/28.
 */
public class TinyTemplateUtil {

    public static NewMetadata findTemplateData(String templateDir){
        String absolutePath= Paths.get(CgformCodeFactory.getClassPath(TinyTemplateUtil.class), CodeResourceUtil.FREEMARKER_CLASSPATH_USERDEFINED,templateDir).toString();
        File templateDataFile= Paths.get(absolutePath,"template.codegen.xml").toFile();
        if (templateDataFile.exists()){
            return readFromXml(templateDataFile);
        }
        return null;
    }

    public static NewMetadata readFromXml(File file){
        XStream xStream = XStreamFactory.getXStream();
        xStream.setClassLoader(CgformCodeFactory.class.getClassLoader());
        xStream.autodetectAnnotations(true);
        xStream.processAnnotations(NewMetadata.class);
        NewMetadata metaData = (NewMetadata) xStream.fromXML(file);
        return metaData;
    }

}
